@echo off
REM Build Pipit project and manually copy output to bin directory

REM Create bin directory if it doesn't exist
if not exist "bin" mkdir "bin"

REM Clean previous build files
cargo clean

REM Build main project with C stdlib enabled
powershell -Command "Write-Host 'Building Pipit project with C stdlib enabled...' -ForegroundColor Green"
cargo build --release --features "use_c_stdlib"

REM Check if build succeeded
if %ERRORLEVEL% neq 0 (
    powershell -Command "Write-Host 'Build failed!' -ForegroundColor Red"
    pause
    exit /b %ERRORLEVEL%
)

REM Copy executable to bin directory
powershell -Command "Write-Host 'Copying executable to bin directory...' -ForegroundColor Green"
if exist "target\release\pipit.exe" (
    copy "target\release\pipit.exe" "bin\pipit.exe" /Y
) else if exist "target\release\deps\pipit.exe" (
    copy "target\release\deps\pipit.exe" "bin\pipit.exe" /Y
) else (
    powershell -Command "Write-Host 'Executable not found in target\release or target\release\deps!' -ForegroundColor Red"
    pause
    exit /b 1
)

REM Check if copy succeeded
if %ERRORLEVEL% neq 0 (
    powershell -Command "Write-Host 'Failed to copy executable!' -ForegroundColor Red"
    pause
    exit /b %ERRORLEVEL%
)

REM Copy DLL files to bin directory
powershell -Command "Write-Host 'Copying DLL files to bin directory...' -ForegroundColor Green"
if exist "stdlib\c\lib\libpipit_stdlib.dll" (
    copy "stdlib\c\lib\libpipit_stdlib.dll" "bin\libpipit_stdlib.dll" /Y
) else (
    powershell -Command "Write-Host 'DLL file not found!' -ForegroundColor Yellow"
)

powershell -Command "Write-Host 'Build and copy completed successfully! Executable and DLLs are in bin directory.' -ForegroundColor Green"
pause